package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.HospitalDao;
import com.itheima.dao.OrderDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Hospital;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 *Author:TH
 *Dete:2019/11/214:19
 */
@Service(interfaceClass = HospitalService.class)
@Transactional
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    private HospitalDao hospitalDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private JedisPool jedisPool;
    //
    public void add(Hospital hospital) {
        //添加信息到数据库
        hospitalDao.add(hospital);
        //添加图片数据到redis
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES, hospital.getImg());
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Hospital> page = hospitalDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void edit(Hospital hospital) {
        hospitalDao.edit(hospital);
    }

    @Override
    public Hospital findById(Integer id) {
        return hospitalDao.findById(id);
    }

    @Override
    public List<Hospital> findAll() {
        return hospitalDao.findAll();
    }

    @Override
    public Result delete(Integer id) {
        List<Integer> list = hospitalDao.findAssociation(id);
        if (list!=null&&list.size()>0){
            //医院存在订单无法删除
            /*List<Map> mapList = new ArrayList<>();
            for (Integer oid : list) {
                Map detail = orderDao.findById4Detail(oid);
                mapList.add(detail);
            }*/
            return new Result(false, MessageConstant.DELETE_HOSPITAL_FAIL,list);
        }else{
            //医院没有订单,直接删除
            hospitalDao.delectById(id);
            return new Result(true,MessageConstant.DELETE_HOSPITAL_SUCCESS);
        }
    }

    @Override
    public void addAddress(Integer orderId, Integer adderssId) {
        Map<String,Integer> map = new HashMap<>();
        map.put("orderId",orderId);
        map.put("addressId",adderssId);
        hospitalDao.addAddress(map);

    }

    @Override
    public  List<Map<String,Object>> findAllAddr() {
        List<Hospital> hospitals = hospitalDao.findAll();
        List<Map<String,Object>> mapList = new ArrayList<>();
         for (Hospital hospital : hospitals) {
             Map<String,Object> map = new HashMap<>();
             map.put("lng",hospital.getLng());
             map.put("lat",hospital.getLat());
             map.put("disc",hospital.getDisc());
             map.put("name",hospital.getName());
            mapList.add(map);
        }
        return mapList;
    }
}
